package m3;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class Consumer {
    public static void main(String[] args) throws MQClientException {
        //创建消费者对象
        DefaultMQPushConsumer c = new DefaultMQPushConsumer("cons-group1");
        //设置name server
        c.setNamesrvAddr("192.168.64.131:9876");
        //设置从哪里订阅消息
        //标签: *-->所有便签, TagA-->只接收TagA,  TagA || TagB || TagC-->接收三个

        c.subscribe("Topic3","*" );
        //设置消息监听器
        //Concurrently--->会启动多个线程处理消息
        c.setMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt ext : msgs) {
                    String s = new String(ext.getBody());
                    System.out.println("收到: " +s);
                }
                //return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                return ConsumeConcurrentlyStatus.RECONSUME_LATER;//稍后重消费
            }
        });
        //启动
        c.start();
    }
}
